import { selectMonitorhcRecord } from "../../service/api/api-monitor";

Page({
  data: {
    id: null,
    details: {},
    sensitives: [],
    filterType: 'all',
    drawerVisible: false,
    loading: true
  },

  onLoad(options) {
    // 接收传入的ID参数
    const id = options.id;
    this.setData({ id });
    
    // 加载数据
    this.loadSelectMonitorhcRecord();
  },

  // 加载数据
  async loadSelectMonitorhcRecord() {
    this.setData({ loading: true });
    
    try {
      // 调用API获取数据
      const res = await selectMonitorhcRecord({
        id: this.data.id,
      });
      
      if (res.code === 0) {
        this.setData({
          details: res.data,
          sensitives: res.data.greenVideo || [],
          loading: false
        });
      } else {
        wx.showToast({
          title: '数据加载失败',
          icon: 'none'
        });
        this.setData({ loading: false });
      }
    } catch (error) {
      wx.showToast({
        title: '请求失败',
        icon: 'none'
      });
      this.setData({ loading: false });
    }
  },

  // 打开抽屉
  openDrawer() {
    this.setData({ drawerVisible: true });
  },

  // 关闭抽屉
  closeDrawer() {
    this.setData({ drawerVisible: false });
  },

  // 筛选类型变化
  onFilterChange(e) {
    const type = e.detail.value;
    this.setData({ filterType: type });
    
    if (type === 'all') {
      this.setData({ sensitives: this.data.details.greenVideo || [] });
    } else {
      const list = (this.data.details.greenVideo || []).filter(item => item.type === type);
      this.setData({ sensitives: list });
    }
  },

  // 跳转到指定时间点
  jumpToTime(e) {
    const time = e.currentTarget.dataset.time;
    const videoContext = wx.createVideoContext('myVideo');
    
    // 转换时间格式 "MM:ss" 到秒数
    const timeParts = time.split(':');
    const seconds = parseInt(timeParts[0]) * 60 + parseInt(timeParts[1]);
    
    videoContext.seek(seconds);
    
    wx.showToast({
      title: `跳转到 ${time}`,
      icon: 'success',
      duration: 1500
    });
    
    // 关闭抽屉
    this.setData({ drawerVisible: false });
  }
})